package edu.uic.cs.i422.s2011.team4;

import java.util.ArrayList;

import edu.uic.cs.i422.s2011.team4.SquirrelData.TreeType;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.text.Editable;
import android.text.TextWatcher;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.LinearLayout;
import android.widget.RadioButton;
import android.widget.RadioGroup;
import android.widget.RadioGroup.OnCheckedChangeListener;

public class TreeObservation extends Activity implements OnClickListener {
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		  
		      View inflatedView = View.inflate(this, R.layout.trees, null);
		        SquirrelUIGenerator squirrel = new SquirrelUIGenerator(this);
		        squirrel.setTitle(R.string.treeTitle);
		        squirrel.setHelp(R.string.treeHelp);
		        squirrel.setBreadCrumb(R.string.treeBreadCrumb);
		        LinearLayout layout = new LinearLayout(this);
		        layout.setOrientation(LinearLayout.VERTICAL); //Set Orientation
		        layout.addView(squirrel.getHeader());
		        layout.addView(inflatedView);
		        setContentView(layout);
		        
		        final  Button doneButton = (Button) findViewById(R.id.tree_done);
		        final EditText editText = (EditText)findViewById(R.id.treeInputText);
		        final RadioGroup radioGroup1 = (RadioGroup)findViewById(R.id.RadioGroup04);
		        final RadioGroup radioGroup2 = (RadioGroup)findViewById(R.id.RadioGroup03);
		        final RadioGroup radioGroup3 = (RadioGroup)findViewById(R.id.RadioGroup02);
		        final RadioGroup radioGroup4 = (RadioGroup)findViewById(R.id.RadioGroup01);
		        final RadioGroup radioGroup5 = (RadioGroup)findViewById(R.id.radioGroup1);
		        
		        ArrayList<TreeType> data = SquirrelData.getTreeTypes();
		        //editText.setText(SquirrelData.getTreeTypes()); TODO
		        setRadioButtons(radioGroup1,data.get(0).getValue().toString());
		        setRadioButtons(radioGroup2,data.get(1).getValue().toString());
		        setRadioButtons(radioGroup3,data.get(2).getValue().toString());
		        setRadioButtons(radioGroup4,data.get(3).getValue().toString());
		        setRadioButtons(radioGroup5,data.get(4).getValue().toString());
		        
		       editText.setText(SquirrelData.getAdditionalTreeData());
		        editText.addTextChangedListener(new TextWatcher(){

					public void afterTextChanged(Editable s) {
						SquirrelData.setTreeType("Other",editText.getText().toString());
					}

					public void beforeTextChanged(CharSequence s, int start,
							int count, int after) {
						// TODO Auto-generated method stub
						
					}

					public void onTextChanged(CharSequence s, int start,
							int before, int count) {
						// TODO Auto-generated method stub
						
					}});
		        radioGroup1.setOnCheckedChangeListener(new OnCheckedChangeListener() {
		                    public void onCheckedChanged(RadioGroup group, int checkedId) {
		                       
		                        SquirrelData.setTreeType("Nut", ((RadioButton) findViewById(checkedId)).getText().toString().toUpperCase());
		                    }
		        }
		        );
		        
		        radioGroup2.setOnCheckedChangeListener(new OnCheckedChangeListener() {
                    public void onCheckedChanged(RadioGroup group, int checkedId) {
                       
                        SquirrelData.setTreeType("Seed", ((RadioButton) findViewById(checkedId)).getText().toString().toUpperCase());
                    }
        }
        );
		        
		        radioGroup3.setOnCheckedChangeListener(new OnCheckedChangeListener() {
                    public void onCheckedChanged(RadioGroup group, int checkedId) {
                       
                        SquirrelData.setTreeType("Fruit", ((RadioButton) findViewById(checkedId)).getText().toString().toUpperCase());
                    }
        }
        );
		        
		        radioGroup4.setOnCheckedChangeListener(new OnCheckedChangeListener() {
                    public void onCheckedChanged(RadioGroup group, int checkedId) {
                       
                        SquirrelData.setTreeType("Tiny", ((RadioButton) findViewById(checkedId)).getText().toString().toUpperCase());
                    }
        }
        );
		        
		        radioGroup5.setOnCheckedChangeListener(new OnCheckedChangeListener() {
                    public void onCheckedChanged(RadioGroup group, int checkedId) {
                       
                        SquirrelData.setTreeType("Coniferous", ((RadioButton) findViewById(checkedId)).getText().toString().toUpperCase());
                    }
        }
        );
		        
				doneButton.setOnClickListener(this);

    }

	public void onClick(View v) {
		Intent intent = new Intent(v.getContext(), FeedingObservation.class);
		startActivityForResult(intent, 0);
	}
	
	protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        if (requestCode == 0) {
            if (resultCode == RESULT_OK) {
            	setResult(RESULT_OK);
                finish();
            }
        }
    }
	
	public void setRadioButtons(RadioGroup group, String value){
		RadioButton mButton = null;
		if(value.equals("YES")){
			mButton = (RadioButton) group.getChildAt(0);
		}
		else if(value.equals("NO")){
			mButton = (RadioButton) group.getChildAt(1);
		}
		else if(value.equals("UNSURE")){
			mButton = (RadioButton) group.getChildAt(2);
		}
		mButton.toggle();
	}
	
}
